# -*- coding: utf-8 -*-
from openerp.osv import fields,osv
from openerp import tools
from openerp.tools.translate import _
from openerp.modules.registry import RegistryManager
import datetime

CHARGE_TYPES = [
    ('normal','普通费用'),
    ('deposit','押金'),
    ('advances','预收款'),
    ('outbuildings','附属建筑费用')
]

class pm_charge(osv.osv):
    _name = 'pm.charge'
    _columns = {
        'name':fields.char(string='流水号',size=100,required=True),
        'charge_type':fields.selection(CHARGE_TYPES,string='收费类别'),
        'project_id':fields.many2one('pm.project',string='所属管理处'),
        'area_id':fields.many2one('pm.project.area',string='所属区域',domain="[('project_id','=',project_id)]"),
        'building_id':fields.many2one('pm.project.building',string='所属楼栋',help='Building',domain="[('project_area_id','=',area_id)]"),
        'room_id':fields.many2one('pm.project.building.room',string='所属房间',required=True,domain="[('building_id','=',building_id)]"),
        'partner_id':fields.many2one('res.partner',string='客户名称'),
        'receivable_amount':fields.float(string='应收金额'),
        'actual_receive_amount':fields.float(string='实收金额'),
        'late_payment_amount':fields.float('滞纳金'),
        'discount_amount':fields.float('优惠金额'),
        'last_left_amount':fields.float('上次余额'),
        'current_left_amount':fields.float('本次余额'),
        'payment_method':fields.many2one('pm.parameter',string='付款方式',domain="[('category_code','=','JFFS')]"),
        'handledby':fields.many2one('res.users',string='经手人'),
        'in_and_out_type':fields.selection([('in','收入'),('out','支出')],string='收支状态'),
        'input_man':fields.many2one('res.users',string='录入人'),
        'input_time':fields.datetime(string='录入时间'),
        'check_man':fields.many2one('res.users',string='审核人'),
        'check_time':fields.datetime(string='审核时间'),
        'state':fields.selection([('recorded','已录入'),('checked','已审核')],string='审核状态'),
        'invoice_state':fields.selection([('yes','已经开票'),('no','尚未开票')],string='开票状态'),
        'need_invoice':fields.boolean(string='是否开票'),
        'invoice_header':fields.char(string='发票抬头',size=300),
        'invoice_content':fields.char(string='发票内容',size=500),
        'invoice_amount':fields.float(string='发票金额'),
        'invoices':fields.one2many('pm.charge.invoice','charge_id',string='相关发票'),
        'remark':fields.text(string='收款备注'),
    }

    _defaults = {
        "input_man":lambda self,cr,uid,context:uid,
        "input_time":lambda self,cr,uid,context:datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        'state':lambda self,cr,uid,context:'recorded'
    }
pm_charge()

class pm_charge_invoice(osv.osv):
    """
    开票完成后可以将发票回填此处
    """
    _name = 'pm.charge.invoice'
    _columns = {
        'charge_id':fields.many2one('pm.charge',string='费用流水号'),
        'name':fields.char(string='发票号',size=100,required=True),
        'header':fields.char(string='发票抬头',size=300),
        'content':fields.char(string='发票内容',size=500),
        'amount':fields.float(string='发票金额'),
        'remark':fields.text(string='开票备注'),
    }
pm_charge_invoice()

